#include "nanocorr.h"
#include "debuglog.h"
#define MIN_NANO_LEN (sizeof(NANO_FRAME))

NANO_PROBE_FLOAT nano_probes[6];
char nano_probes_cnt;
NANO_CP_PARAM_FLOAT nano_cp_param;

unsigned char nano_checksum(unsigned char *buf,unsigned char len)
{
    unsigned short sum = buf[0];
    unsigned char i;
    for(i=1;i<len;i++){
        sum += buf[i];
        if(sum & 0xff00){
            sum = (sum & 0xff) + ((sum >> 8) & 0xff);
        }
    }
    return ~(sum & 0xff);
}
void nano_probe_byte2float(NANO_PROBE *pprobe,NANO_PROBE_FLOAT *pprobe_f)
{
    pprobe_f->chuan_kong_su_lv = (pprobe->chuan_kong_su_lv[0] << 16 | pprobe->chuan_kong_su_lv[1] << 8 | pprobe->chuan_kong_su_lv[2])/10.0;
    pprobe_f->ying_bao_you_xiao_du = ((pprobe->ying_bao_you_xiao_du[0] << 8 | pprobe->ying_bao_you_xiao_du[1]) / 10.0) - 10.0;
    pprobe_f->ping_jun_su_lv = (pprobe->ping_jun_su_lv[0] << 16 | pprobe->ping_jun_su_lv[1] << 8 | pprobe->ping_jun_su_lv[2])/10.0;
     pprobe_f->chuan_kong_shen_du = (pprobe->chuan_kong_shen_du[0] << 16 | pprobe->chuan_kong_shen_du[1] << 8 | pprobe->chuan_kong_shen_du[2])/1000.0;
    pprobe_f->tan_tou_dian_wei = ((pprobe->tan_tou_dian_wei[0] << 8 | pprobe->tan_tou_dian_wei[1]) / 1000.0)- 5.0;
    pprobe_f->tan_tou_ping_jun_dian_liu = ((pprobe->tan_tou_ping_jun_dian_liu[0] << 16 | pprobe->tan_tou_ping_jun_dian_liu[1] << 8 | pprobe->tan_tou_ping_jun_dian_liu[2]) / 1000.0) - 5000.0;
}
void nano_cp_param_byte2float(NANO_CP_PARAM *pcpparam,NANO_CP_PARAM_FLOAT *pcpparam_f)
{
    log_info("%02x %02x",pcpparam->E_off[0],pcpparam->E_off[1]);
    pcpparam_f->E_off = ((pcpparam->E_off[0] << 8 | pcpparam->E_off[1]) )/1000.0 - 20;
    pcpparam_f->E_on = ((pcpparam->E_on[0] << 8 | pcpparam->E_on[1]) )/1000.0 - 20;
    pcpparam_f->E_corr = ((pcpparam->E_corr[0] << 8 | pcpparam->E_corr[1]))/1000.0 -5;
    pcpparam_f->V_rms = ((pcpparam->V_rms[0] << 8 | pcpparam->V_rms[1]) ) / 1000.0;
    pcpparam_f->I_rms = ((pcpparam->I_rms[0] << 8 | pcpparam->I_rms[1]) ) / 100.0;
    pcpparam_f->I_dc = (pcpparam->I_dc[0] << 16 | pcpparam->I_dc[1] << 8 | pcpparam->I_dc[2])/1000.0 - 200.0;
}
void dumps_nano(NANO_PROBE_FLOAT *pprobe_f)
{
    log_info("chuan_kong_su_lv %f",pprobe_f->chuan_kong_su_lv);
    log_info("ying_bao_you_xiao_du %f",pprobe_f->ying_bao_you_xiao_du);
    log_info("ping_jun_su_lv %f",pprobe_f->ping_jun_su_lv);
    log_info("chuan_kong_shen_du %f",pprobe_f->chuan_kong_shen_du);
    log_info("tan_tou_dian_wei %f",pprobe_f->tan_tou_dian_wei);
    log_info("tan_tou_ping_jun_dian_liu %f",pprobe_f->tan_tou_ping_jun_dian_liu);
}
void dumps_cpparam(NANO_CP_PARAM_FLOAT *pcpparam_f)
{
    log_info("E_off %f",pcpparam_f->E_off);
    log_info("E_on %f",pcpparam_f->E_on);
    log_info("E_corr %f",pcpparam_f->E_corr);
    log_info("V_rms %f",pcpparam_f->V_rms);
    log_info("I_rms %f",pcpparam_f->I_rms);
    log_info("I_dc %f",pcpparam_f->I_dc);
}
char check_nano_format(unsigned char *buf,unsigned char len,NANO_FRAME **p2)
{
    NANO_FRAME *p;
    unsigned char i,checksum,checksum2;
    
    if(len < MIN_NANO_LEN){
        log_info("len too short");
        return 0;
    }
    for(i = 0;i<len-2;i++){
        if(buf[i] == ADDR_H && buf[i+1] == ADDR_L && buf[i+2] == READ_NANO)
            break;
    }
    if(len - i < MIN_NANO_LEN){
        log_info("len too short 2");
        return 0;
    }
    p = (NANO_FRAME *)(buf+i);
    checksum = nano_checksum((unsigned char *)&p->nano_msg_time_cnt,p->nano_msg_head.msg_len - 1);
    checksum2 = *(buf+i+sizeof(NANO_MSG_HEAD)+p->nano_msg_head.msg_len - 1);
    if(checksum != checksum2){
        log_info("check sum error %02x %02x",checksum,checksum2);
        return 0;
    }
    *p2 = p;
    return 1;
}

void main(void)
{
    NANO_FRAME *p;

    char i;
    unsigned char sample_data[] = {0x39,0x39 ,0x31, 0x26, 0x20, 0x20, 0x01, 0x13 ,0x15 ,0x17 ,0x58 ,0x01 ,0x00 ,0x6C ,0x05 ,0x03 ,0xE1 ,0x00 ,0x22 ,0xF9 ,0x98 ,0x96 ,0x7F ,0x21 ,0x61 ,0x4C ,0x49 ,0xDB ,0x4C ,0x08 ,0x4B ,0x0A ,0x0F ,0xAA ,0x01 ,0xCA ,0x30 ,0x34 ,0x02 ,0xF8 ,0x3C ,0x46};
    unsigned char s2[] ={
        0x39,0x39 ,0x31 ,0x36 ,0x20 ,0x20 ,0x01 ,0x13 ,0x15 ,0x17 ,0x58 ,0x02 ,0x00 ,0x6C ,0x05 ,0x03 ,0xE1 ,0x00 ,0x22 ,0xF9 ,0x98 ,0x96 ,0x7F ,0x21 ,0x61 ,0x4C ,0x49 ,0xDB ,0x00 ,0x6C ,0x05 ,0x03 ,0xE1 ,0x00 ,0x22 ,0xF9 ,0x98 ,0x96 ,0x7F ,0x21 ,0x61 ,0x4C ,0x49 ,0xDB ,0x4C ,0x08 ,0x4B ,0x0A ,0x0F ,0xAA ,0x01 ,0xCA ,0x30 ,0x34 ,0x02 ,0xF8 ,0x3C ,0x30
    };
    
    unsigned char flag = check_nano_format(s2,sizeof(s2),&p);
    if(flag){
        nano_probes_cnt = 0;
        for(i=0;i<p->nano_msg_time_cnt.probe_cnt;i++){
            nano_probe_byte2float((NANO_PROBE *)(((unsigned char *)&p->nano_probes)+i*sizeof(NANO_PROBE)), (NANO_PROBE_FLOAT *)(nano_probes + i*sizeof(NANO_PROBE)));
            nano_probes_cnt++;
        }
        nano_cp_param_byte2float((NANO_CP_PARAM *)((unsigned char *)&p->nano_probes+nano_probes_cnt*sizeof(NANO_PROBE)),(NANO_CP_PARAM_FLOAT *)&nano_cp_param);
        log_info("probe_cnt %d",nano_probes_cnt);
        for(i=0;i<nano_probes_cnt;i++)
            dumps_nano((NANO_PROBE_FLOAT *)(nano_probes + i*sizeof(NANO_PROBE)));
        dumps_cpparam((NANO_CP_PARAM_FLOAT *)&nano_cp_param);
    }
}
